package com.fingard.dsp.bank.directbank.lu11;

import java.io.File;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.fingard.constant.Format;
import com.fingard.dsp.bank.directbank.bankInterface.IAcceptNotice;
import com.fingard.dsp.bank.reqrespobj.statement.TransState;
import com.fingard.dsp.bank.reqrespobj.statement.detail.RetAtsStatementDetail;
import com.fingard.generalobj.NoticeReq;
import com.fingard.generalobj.NoticeResp;
import com.fingard.io.FileHelper;
import com.fingard.net.listener.SocketReceiver;
import com.fingard.xml.XmlTextWriter;

public class LU11AcceptNotice extends LU11Base implements IAcceptNotice {
	@Override
	public NoticeResp dealNotice(NoticeReq pNtReq) throws Exception {
		
    	NoticeResp resp = new NoticeResp();
    	String p_req = pNtReq.getReqString();
    	WriteBankLogLn("承保/退保通知推送原始数据:\r\n" + pNtReq.getFullString());
		if (p_req.contains("{")) {
			p_req = p_req.substring(p_req.indexOf("{"));
		}
		WriteBankLogLn("承保/退保通知推送有效数据:\r\n" + p_req);
		
		JSONObject tmpRespJson = JSONObject.parseObject(p_req);
		
		String transType = tmpRespJson.getString("confirmType");
		if ("1".equals(transType)) {
			transType = "rec_underwrite";//承保
		} else {
			transType = "pay_surrender";//退保
		}
		String tmpPayType = "rec_underwrite".equals(transType) ? "承保" : "退保";
		WriteBankLogLn("当前交易类型:" + tmpPayType);
		
		XmlTextWriter retXmlWriter = new XmlTextWriter();
		retXmlWriter.writeStartElement("LU11");
		if(tmpRespJson.containsKey("orderList")){
		JSONArray orderList = JSONArray.parseArray(tmpRespJson.getString("orderList"));
		if(orderList.size()>0){
		for(int i = 0; i < orderList.size(); i++){
            JSONObject orderDetail = orderList.getJSONObject(i);
            
            retXmlWriter.writeStartElement("DetailRecord");
            RetAtsStatementDetail tmpRetDetail = new RetAtsStatementDetail();
            // 承保
            if ("rec_underwrite".equals(transType)) {
            	tmpRetDetail.cdFlag = "22";
            	tmpRetDetail.askNumber = (String) orderDetail.get("orderId");
            	tmpRetDetail.srcNote = (String) orderDetail.get("policyNo");
            	tmpRetDetail.bkSeq = (String) orderDetail.get("payOrderId");
            	tmpRetDetail.amount = orderDetail.get("investAmount").toString();
            	tmpRetDetail.busTime = Format.DateTime14Format.format(Format.DateTimeFormat.parse((String) orderDetail.get("payTime")));
            	tmpRetDetail.transState = TransState.Success;
            } else {
            	tmpRetDetail.cdFlag = "27";
            	tmpRetDetail.askNumber = (String) orderDetail.get("orderId");
            	tmpRetDetail.srcNote = (String) orderDetail.get("policyNo");
            	tmpRetDetail.amount = orderDetail.get("refundAmount").toString();
            	tmpRetDetail.bkRefundSeq = (String) orderDetail.get("refundProposalNo");
            	tmpRetDetail.busTime = Format.DateTime14Format.format(Format.DateTimeFormat.parse((String)orderDetail.get("confirmDate")));
            	tmpRetDetail.transState = TransState.Success;
            }
            tmpRetDetail.writeXE(retXmlWriter);
    		retXmlWriter.writeEndElement();
        }
		retXmlWriter.writeEndDocument();
		WriteBankLogLn("待存入本机明细响应数据:\r\n" + retXmlWriter.toXmlString());
		
		String tmpResultFileName = transType + "_" + Format.Date8Format.format(Format.Date10Format.parse(tmpRespJson.getString("confirmDate"))) + ".xml";
		String tmpResultFilePath = getNotifyDataDirectory() + File.separator + tmpResultFileName;
		WriteBankLogLn("待存入本机明细数据路径:\r\n" + tmpResultFilePath);
		
		FileHelper.writeFile(tmpResultFilePath, retXmlWriter.toXmlString(), "UTF-8");
		}else{
			WriteBankLogLn("陆金所未推送数据");
		}
		}
		String tmpStrRet = "{\"res_code\":\"0000\",\"res_msg\":\"接收成功\"}";
        WriteBankLogLn("响应："+ tmpStrRet);
        resp.respBytes = tmpStrRet.getBytes(pNtReq.reqCharset);
    	return resp;
	}

	@Override
	public void getContentLength(SocketReceiver p_receiver) throws Exception {

	}
}
